#coding=utf-8
from selenium import webdriver
import time
import random
from PIL import Image
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait as WD
from selenium.webdriver.common.by import By
from ShowapiRequest import ShowapiRequest

driver = webdriver.Chrome()
driver.get("http://www.5itest.cn/register")
driver.maximize_window()
time.sleep(5)
print(EC.title_contains("注册"))
email_element = driver.find_element_by_id("register_email")
#将网页以png格式的图片保存
driver.save_screenshot("D:/heqq/web/moke3.png")
#获取到验证码的图片元素id
code_element = driver.find_element_by_id("getcode_num")
#用location方法获取x和y的值
print(code_element.location)
left = code_element.location['x']
top = code_element.location['y']
#用size方法获取width和height的值加上前面获取的坐标值可以计算出剩下两个点的值
width = code_element.size['width'] + left
height = code_element.size['height'] + top
#使用open方法打开前面保存的moke.png之后跟据坐标用crop方法获得图片位置后使用save方法保存
im = Image.open("D:/heqq/web/moke3.png")
img = im.crop((left,top,width,height))
img.save("D:/heqq/web/moke4.png")
#通过第三方验证码api读出验证码
r = ShowapiRequest("http://route.showapi.com/184-4","101290","bff0fe2ab4a743538879d2f187ace8f1" )
r.addFilePara("image", "D:/heqq/web/moke4.png")
r.addBodyPara("typeId", "35")
r.addBodyPara("convert_to_jpg", "0")
r.addBodyPara("needMorePrecise", "0")
res = r.post()
text = res.json()['showapi_res_body']['Result']
print(text) # 返回信息
driver.find_element_by_id("captcha_code").send_keys(text)
time.sleep(5)
driver.close


